package com.service.core.rest;

import com.github.pagehelper.Page;
import com.service.core.bean.CommonRep;
import com.service.core.constant.BusinessStatus;
import com.service.core.rest.response.*;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.service.core.constant.BusinessStatus.*;


/**
 * Created by yezhang on 7/14/2017.
 */
public class BaseController {


    //--------------- success ----------------

    protected BaseResponse successSaveBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    protected BaseResponse successDeleteBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    protected BaseResponse successUpdateBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    protected BaseResponse successQueryBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    protected <T> OneResponse<T> successQueryOneResponse(T t){
        return new OneResponse<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), t);
    }

    protected <T> PageResponse<T> successQueryPageResponse(CommonPageRep<T> pageRep) {
        return new PageResponse<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageRep);
    }

    protected <T> MultiResponse<T> successQueryMultiResponse(List<T> data) {
        return new MultiResponse<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), data);
    }

    protected BaseResponse successBaseResponse(String desc){
        return new BaseResponse(HttpStatus.OK.value(), desc);
    }

    protected BaseResponse successBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    //--------------- token error ----------------

    protected BaseResponse tokenErrorBaseResponse(HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(TOKEN_ERROR.value(), TOKEN_ERROR.getReasonPhrase());
    }

    //--------------- 500 server error ----------------

    protected BaseResponse serverExceptionBaseResponse(HttpServletResponse response) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        return new BaseResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    }

    //--------------- 404 not found ----------------

    protected BaseResponse notFoundBaseResponse(String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new BaseResponse(HttpStatus.NOT_FOUND.value(), desc);
    }

    protected BaseResponse notFoundBaseResponse(Integer status, String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new BaseResponse(status, desc);
    }

    protected <T> OneResponse<T> notFoundOneResponse(String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new OneResponse<>(HttpStatus.NOT_FOUND.value(), desc, null);
    }

    protected <T> OneResponse<T> notFoundOneResponse(Integer status, String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new OneResponse<>(status, desc, null);
    }

    //--------------- 400 ----------------

    protected BaseResponse invalidRequestBaseResponse(HttpServletResponse response){
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(HttpStatus.BAD_REQUEST.value(), "无效请求");
    }

    //--------------- 401 ----------------

    protected BaseResponse unauthorizedBaseResponse(String desc, HttpServletResponse response) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        return new BaseResponse(HttpStatus.UNAUTHORIZED.value(), StringUtils.isEmpty(desc) ? "未授权访问" : desc);
    }

    //--------------- fail ----------------

    protected BaseResponse notSupportFileTypeBaseResponse(HttpServletResponse response){
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(NOT_SUPPORT_FILE_TYPE.value(), NOT_SUPPORT_FILE_TYPE.getReasonPhrase());
    }

    protected BaseResponse notSupportServiceNameBaseResponse(HttpServletResponse response){
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(INVALID_SERVICE_NAME.value(), INVALID_SERVICE_NAME.getReasonPhrase());
    }

    //--------------- delete self error ----------------

    protected BaseResponse deleteSelfErrorBaseResponse(HttpServletResponse response){
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(DELETE_SELF_ERROR.value(), DELETE_SELF_ERROR.getReasonPhrase());
    }

    //--------------- buildin base ----------------

    protected BaseResponse deleteBuildinSystemErrorBaseResponse(String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(DELETE_BUILD_IN_SYSTEM_ERROR.value(), desc);
    }

    //--------------- existed identifier ----------------

    protected BaseResponse existedIdentifierBaseResponse(String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(EXISTED_IDENTIFIER.value(), desc);
    }

}
